package com.dream.myqiyi.db;

import java.io.File;
import java.sql.SQLException;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dream.myqiyi.BaseApp;
import com.dream.myqiyi.Global;
import com.dream.myqiyi.dao.NoteModel;
import com.dream.myqiyi.dao.NoteResourceModel;
import com.dream.myqiyi.dao.NoteTagsModel;
import com.dream.myqiyi.dao.NotebookModel;
import com.dream.myqiyi.dao.TagModel;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;

public class DBManager {
	
 	private MainDBHelper mainDB = null;
 	
 	private NotebookDaoImpl notebookDao = null;
 	private NoteDaoImpl noteDao = null;
 	private NoteResourceDaoImpl noteResDao = null;
 	private NoteTagsDaoImpl noteTagsDao = null;
 	private TagDaoImpl tagDao = null;
 	
 	private boolean isRemote = false;

 	public DBManager() {
  	}
 	
 	public boolean init() {
 		if (Global.IS_REMOTE_DB) {
 			return dbRemoteSetup();
 		} else {
 			return dbSetup();
 		}
	}
 	
 	public boolean dbRemoteSetup() {
 		isRemote = true;
 		ConnectionSource connectionSource = null;
 		
 		try {   
 		    //加载MySql的驱动类   
 		    Class.forName("com.mysql.jdbc.Driver") ;   
 		}catch(ClassNotFoundException e){
 			BaseApp.showToast("程序数据被破坏， 请重新安装程序！");
 			return false;  
 		}   
 		try {
 			connectionSource = new JdbcConnectionSource(Global.REMOTE_DB_URL);
 			((JdbcConnectionSource)connectionSource).setUsername(Global.DB_USERNAME);
 			((JdbcConnectionSource)connectionSource).setPassword(Global.DB_PASSWORD);
 			
 			notebookDao = new NotebookDaoImpl(connectionSource);
 			noteDao = new NoteDaoImpl(connectionSource);
 			noteResDao = new NoteResourceDaoImpl(connectionSource);
 			noteTagsDao = new NoteTagsDaoImpl(connectionSource);
 			tagDao = new TagDaoImpl(connectionSource);

 			notebookDao.performDBOperations(connectionSource);
 			noteDao.performDBOperations(connectionSource);
 			noteResDao.performDBOperations(connectionSource);
 			noteTagsDao.performDBOperations(connectionSource);
 			tagDao.performDBOperations(connectionSource);
 			
 		} catch (SQLException e) {
 			e.printStackTrace(); 			
 			BaseApp.showToast("数据库连接失败， 请检查您的网络！");
 			return false;  
 		}
 		return true;
 	}

	// Initialize the database
	public boolean dbSetup() {
		isRemote = false;
		Log.i("[YiNote]", "Entering DatabaseConnection.dbSetup. ");
		
		File fMain = new File(MainDBHelper.DATABASE_PATH);
        if (!fMain.exists()) {
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
            		MainDBHelper.DATABASE_PATH, null);
            mainDB = MainDBHelper.createSingleton(BaseApp.context);
            mainDB.onCreate(db);
            db.close();
        } else {
        	mainDB = MainDBHelper.createSingleton(BaseApp.context);
        }
				
				
		Log.i("[MyNeverNote]", "Leaving DatabaseConnection.dbSetup. ");
		
		return true;
	}

	// Compact the database
	public void compactDatabase() {
		
	}
	
	public void backupDatabase(int highSequence, long date) {
		
	}

	public void dbShutdown() {
		Log.i("[MyNeverNote]", "Entering RDatabaseConnection.dbShutdown. ");
		mainDB.close();
		Log.i("[MyNeverNote]", "Leaving RDatabaseConnection.dbShutdown. ");
	}
	
	public MainDBHelper getMainDB() {
		return mainDB;
	}

    public Dao<TagModel, String> getTagDao() {
    	if (isRemote) {
    		return tagDao.getDao();
    	} else {
    		return MainDBHelper.getSingleton().getTagDao();
    	}
    }
    public Dao<NotebookModel, String> getNotebookDao() {
    	if (isRemote) {
    		return notebookDao.getDao();
    	} else {
    		return MainDBHelper.getSingleton().getNotebookDao();
    	}
    }
	public Dao<NoteModel, String> getNoteDao() {
    	if (isRemote) {
    		return noteDao.getDao();
    	} else {
    		return MainDBHelper.getSingleton().getNoteDao();
    	}
	}
	public Dao<NoteTagsModel, Integer> getNoteTagsDao() {
    	if (isRemote) {
    		return noteTagsDao.getDao();
    	} else {
    		return MainDBHelper.getSingleton().getNoteTagsDao();
    	}
	}
	public Dao<NoteResourceModel, String> getNoteResourceDao() {
    	if (isRemote) {
    		return noteResDao.getDao();
    	} else {
    		return MainDBHelper.getSingleton().getNoteResourceDao();
    	}
	}

}
